/*
Danger from the Deep - Open source submarine simulation
Copyright (C) 2003-2006  Thorsten Jordan, Luis Barrancos and others.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// a cross section measurement tool
// subsim (C)+(W) Thorsten Jordan. SEE LICENSE

#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

#include <gl.h>
#include <glu.h>
#include <SDL.h>

#include "system.h"
#include "vector3.h"
#include "datadirs.h"
#include "font.h"
#include "model.h"
#include "texture.h"
#include <iostream>
#include <sstream>

#include "mymain.cpp"

class system* mysys;
int res_x, res_y;
model* mdl;
double screenarea_meters;
double ztrans, mw, mh;
unsigned ANGLES = 256;



void draw_model(double angle)
{
	glPushMatrix();
	glTranslated(res_x/2, 0, 0);
	glScalef(double(res_x)/mw, double(res_y)/mh, 0.0);
	glRotatef(angle, 0, 1, 0);
	glRotated(-90.0, 1, 0, 0);
	mdl->display();
	glPopMatrix();
	glColor3f(1,0,0);
	glBegin(GL_LINES);
	glVertex2d(0,0);
	glVertex2d(10,10);
	glEnd();
	glColor3f(1,1,1);
}

void measure_model(double angle)
{
	glClear(GL_COLOR_BUFFER_BIT);
	draw_model(angle);
	unsigned filledpixels = 0;
	unsigned screen_pixels = res_x*res_y;
	vector<Uint8> pic(screen_pixels*3);
	glReadPixels(0, 0, res_x, res_y, GL_RGB, GL_UNSIGNED_BYTE, &pic[0]);
	for (unsigned i = 0; i < screen_pixels*3; i += 3) {
		// faster counting, uses no conditional jumps.
		filledpixels += pic[i] ? 1 : 0;
	}
	double crosssection = screenarea_meters * filledpixels / screen_pixels;
	cout << crosssection << "\n";
}




int mymain(list<string>& args)
{
	res_x = 1024;
	bool fullscreen = true;
	string modelfilename;
	for (list<string>::iterator it = args.begin(); it != args.end(); ++it) {
		if (*it == "--help") {
			cout << "crosssection, usage:\n--help\t\tshow this\n"
			<< "--res n\t\tuse resolution n horizontal,\n\t\tn is 512,640,800,1024 (recommended) or 1280\n"
			<< "--nofullscreen\tdon't use fullscreen\n--angles n\tmeasure n different angles\n"
			<< "MODELFILENAME\n";
			return 0;
		} else if (*it == "--nofullscreen") {
			fullscreen = false;
		} else if (*it == "--res") {
			list<string>::iterator it2 = it; ++it2;
			if (it2 != args.end()) {
				int r = atoi(it2->c_str());
				if (r==512||r==640||r==800||r==1024||r==1280)
					res_x = r;
				++it;
			}
		} else if (*it == "--angles") {
			list<string>::iterator it2 = it; ++it2;
			if (it2 != args.end()) {
				int r = atoi(it2->c_str());
				ANGLES = r;
				++it;
			}
		} else {
			modelfilename = *it;
		}
	}

	res_y = res_x*3/4;

	mysys = new class system(1.0, 1000.0, res_x, res_y, fullscreen);
	mysys->set_res_2d(1024, 768);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0,res_x, 0,res_y);
	glDisable(GL_LIGHTING);
	glDisable(GL_DEPTH_TEST);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glClearColor(0,0,0,0);
	glColor3f(1.0f, 1.0f, 1.0f);
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);

	mdl = new model(modelfilename, false);
	mdl->register_layout();
	mdl->set_layout();
	vector3f mmin = mdl->get_min();
	vector3f mmax = mdl->get_max();
	ztrans = mmin.z;
	mw = mmax.y - mmin.y;
	mh = mmax.z;
	screenarea_meters = mw * mh;
	cout << ANGLES << "\n";
	for (unsigned i = 0; i < ANGLES; ++i) {
		double angle = 360.0 * i / ANGLES;
		measure_model(angle);

		mysys->poll_event_queue();
	}
	delete mdl;

	delete mysys;

	return 0;
}
